package com.movie.task;

import com.movie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class OrderTimeoutTask {

    @Autowired
    private OrderService orderService;

    /**
     * 每分钟执行一次，自动取消超时未支付的订单
     * cron表达式：秒 分 时 日 月 周
     */
    @Scheduled(cron = "0 */1 * * * *")
    public void cancelTimeoutOrders() {
        int deletedCount = orderService.cancelTimeoutOrders();
        if (deletedCount > 0) {
            System.out.println("定时任务执行：已自动删除 " + deletedCount + " 个超时未支付订单");
        }
    }
}
